package com.hyt.it.ogt.kq.common.utils;

import com.google.common.base.Strings;
import com.hyt.core.util.UUIDUtils;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import static java.lang.Character.UnicodeBlock.*;

/**
 * 继承自Spring util的工具类，减少jar依赖
 *
 * @author L.cm
 */
public class KqStringUtils extends org.springframework.util.StringUtils {
	/**
	 * <p>
	 * Checks if a CharSequence is not empty (""), not null and not whitespace only.
	 * </p>
	 *
	 * <pre>
	 * StringUtils.isNotBlank(null)      = false
	 * StringUtils.isNotBlank("")        = false
	 * StringUtils.isNotBlank(" ")       = false
	 * StringUtils.isNotBlank("bob")     = true
	 * StringUtils.isNotBlank("  bob  ") = true
	 * </pre>
	 *
	 * @param cs the CharSequence to check, may be null
	 * @return {@code true} if the CharSequence is not empty and not null and not
	 *         whitespace
	 * @see Character#isWhitespace
	 */
	public static boolean isNotBlank(final CharSequence cs) {
		return KqStringUtils.hasText(cs);
	}

	/**
	 * Check whether the given {@code CharSequence} contains actual <em>text</em> 
	 * 
	 * More specifically, this method returns {@code true} if the
	 * {@code CharSequence} is not {@code null}, its length is greater than 0, and
	 * it contains at least one non-whitespace character.
	 * 
	 * <pre class="code">
	 * StringUtils.isBlank(null) = true
	 * StringUtils.isBlank("") = true
	 * StringUtils.isBlank(" ") = true
	 * StringUtils.isBlank("12345") = false
	 * StringUtils.isBlank(" 12345 ") = false
	 * </pre>
	 *
	 * @param cs the {@code CharSequence} to check (may be {@code null})
	 * @return {@code true} if the {@code CharSequence} is not {@code null}, its
	 *         length is greater than 0, and it does not contain whitespace only
	 * @see Character#isWhitespace
	 */
	public static boolean isBlank(final CharSequence cs) {
		return !KqStringUtils.isNotBlank(cs);
	}

	/**
	 * Function:判断字符串是否为空或零长
	 *
	 * @param str 待检查的字符串变量
	 * @return true 字符串为空或零长 false 字符串不为空且不是零长字符串
	 */
	public static boolean isBlank(final String str) {
		return (str == null) || (str.trim().length() <= 0);
	}

	/**
	 * Function:判断字符是否为空或空格
	 *
	 * @param cha 待检查的字符变量
	 * @return true 字符为空或空格 false 字符不为空且不是空格
	 */
	public static boolean isBlank(final Character cha) {
		return (cha == null) || cha.equals(' ');
	}

	/**
	 * Function:判断对象是否为空
	 *
	 * @param obj 待检查的对象变量
	 * @return true 对象为空 false 对象不为空
	 */
	public static boolean isBlank(final Object obj) {
		return (obj == null);
	}

	/**
	 * Function:判断对象数组是否为空或无元素
	 *
	 * @param objs 待检查的对象数组变量
	 * @return true 对象数组为空或无元素 false 对象数组不为空且有元素
	 */
	public static boolean isBlank(final Object[] objs) {
		return (objs == null) || (objs.length <= 0);
	}

	/**
	 * Function:判断集合对象是否为空或无元素
	 *
	 * @param obj 待检查的集合对象变量
	 * @return true 集合对象为空或无元素 false 集合对象不为空且有元素
	 */
	public static boolean isBlank(final Collection<?> obj) {
		return (obj == null) || (obj.size() <= 0);
	}

	/**
	 * Function:判断Set对象是否为空或无元素
	 *
	 * @param obj 待检查的Set对象变量
	 * @return true Set对象为空或无元素 false Set对象不为空且有元素
	 */
	public static boolean isBlank(final Set<?> obj) {
		return (obj == null) || (obj.size() <= 0);
	}

	/**
	 * Function:判断Serializable对象是否为空
	 *
	 * @param obj 待检查的Serializable对象变量
	 * @return true Serializable对象为空 false Serializable对象不为空
	 */
	public static boolean isBlank(final Serializable obj) {
		return obj == null;
	}

	/**
	 * Function:判断Map对象是否为空或无元素
	 *
	 * @param obj 待检查的Map对象变量
	 * @return true Map对象为空或无元素 false Map对象不为空且有元素
	 */
	public static boolean isBlank(final Map<?, ?> obj) {
		return (obj == null) || (obj.size() <= 0);
	}

	/**
	 * Convert a {@code Collection} into a delimited {@code String} (e.g. CSV).
	 * Useful for {@code toString()} implementations.
	 *
	 * @param coll  the {@code Collection} to convert
	 * @param delim the delimiter to use (typically a ",")
	 * @return the delimited {@code String}
	 */
	public static String join(Collection<?> coll, String delim) {
		return KqStringUtils.collectionToDelimitedString(coll, delim);
	}

	/**
	 * Convert a {@code String} array into a delimited {@code String} (e.g. CSV).
	 * Useful for {@code toString()} implementations.
	 *
	 * @param arr   the array to display
	 * @param delim the delimiter to use (typically a ",")
	 * @return the delimited {@code String}
	 */
	public static String join(Object[] arr, String delim) {
		return KqStringUtils.arrayToDelimitedString(arr, delim);
	}

	/**
	 * 生成uuid
	 *
	 * @return UUID
	 */
	public static String getUUId() {
		return UUIDUtils.newSortUUID();
	}

	/**
	 * 创建指定数量的随机字符串
	 *
	 * @param numberFlag  是否是数字
	 * @param length 
	 * @return String 
	 */
	public static String generateRandomStr(boolean numberFlag, int length) {
		//  Auto-generated method stub
		String retStr = "";
		String strTable = numberFlag ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
		int len = strTable.length();
		boolean bDone = true;
		do {
			retStr = "";
			int count = 0;
			for (int i = 0; i < length; i++) {
				double dblR = Math.random() * len;
				int intR = (int) Math.floor(dblR);
				char c = strTable.charAt(intR);
				if (('0' <= c) && (c <= '9')) {
					count++;
				}
				retStr += strTable.charAt(intR);
			}
			if (count >= 2) {
				bDone = false;
			}
		} while (bDone);
		return retStr.toUpperCase();
	}

	/**
	 * Function:判断String对象是否为null，如果为null则返回空字符串，否则返回原字符串
	 *
	 * @param str 待检查的String对象变量
	 * @return String
	 */
	public static String handleNullToEmpty(final String str) {
		return isNotBlank(str) ? str : "";
	}

	/**
	 * 判断两个字符串是否相等
	 *
	 * @param str1 
	 * @param str2 
	 * @return boolean 
	 */
	public static boolean isEquls(String str1, String str2) {
		if (isBlank(str1) || isBlank(str2)) {
			return false;
		}
		return str1.equals(str2);
	}
	
	/**
	 * 判断两个字符串是否相等
	 *
	 * @param str1 
	 * @param str2 
	 * @return boolean 
	 */
	public static boolean isNullOrEmpty(String str1) {
		return org.apache.commons.lang3.StringUtils.isEmpty(str1) || "null".equals(str1);
	}
	
	/**
	 * 判断两个字符串是否相等
	 *
	 * @param str1 
	 * @param str2 
	 * @return boolean 
	 */
	public static boolean isNotNullOrNotEmpty(String str1) {
		return org.apache.commons.lang3.StringUtils.isNotEmpty(str1) && !"null".equals(str1) && !"NULL".equals(str1);
	}


	/**
	 * 判断字符串是否包含中文
	 * @param checkStr
	 * @return
	 */
	public static boolean checkStringContainChinese(String checkStr){
		if(!Strings.isNullOrEmpty(checkStr)){
			char[] checkChars = checkStr.toCharArray();
			for(int i = 0; i < checkChars.length; i++){
				char checkChar = checkChars[i];
				if(checkCharContainChinese(checkChar)){
					return true;
				}
			}
		}
		return false;
	}

	private static boolean checkCharContainChinese(char checkChar){
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(checkChar);
		if(CJK_UNIFIED_IDEOGRAPHS == ub || CJK_COMPATIBILITY_IDEOGRAPHS == ub || CJK_COMPATIBILITY_FORMS == ub ||
				CJK_RADICALS_SUPPLEMENT == ub || CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A == ub || CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B == ub){
			return true;
		}
		return false;
	}
}
